// 引入股池.json
const stocks = require("./股池.json");

// 循环stocks，通过 预告净利润行业中值[20241231] - 归属于母公司所有者的净利润[20240930] 得到第四季度净利润;并且按照第四季度净利润是否为正，分为两个数组
const stocks1 = [];
const stocks2 = [];
for (let i = 0; i < stocks.length; i++) {
  const stock = stocks[i];
  const stockCode = stock["股票代码"];
  const stockName = stock["股票简称"];
  const stockForecastProfit = stock["预告净利润[20241231]"];
  const stockProfit = stock["归属于母公司所有者的净利润[20240930]"];
  const stockProfit4 = stockForecastProfit - stockProfit;
  if (stockCode === "601628.SH") {
    console.log(
      stockCode,
      stockName,
      stockForecastProfit,
      stockProfit,
      stockProfit4
    );
  }
  if (stockProfit4 > 0) {
    stocks1.push({ stockCode, stockName, stockProfit4 });
  } else {
    stocks2.push({ stockCode, stockName, stockProfit4 });
  }
}

// 降stocks1和stocks2写入至result.json
const fs = require("fs");
const result = { stocks1, stocks2 };
fs.writeFileSync("result.json", JSON.stringify(result));

// 提取stocks1的股票代码成一个字符串，以换行符割开，写入至stocks1.txt
const stocks1Code = stocks1.map((stock) => stock.stockCode).join("\n");
fs.writeFileSync("stocks1.txt", stocks1Code);

// 得到股票代码以3开头的股票数量
const stocks1Code3 = stocks1.filter((stock) => stock.stockCode.startsWith("3"));
console.log(stocks1Code3.length);
